package com.example.ecas.controller;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.neo4j.driver.*;
import org.neo4j.driver.Record;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Random;


@CrossOrigin
@RestController
public class GenerateGraphController {
    @GetMapping(value="/test")
    public String transferJson(){
        System.out.println("test");
        String filePath="\\practiseTraining\\ecasProject2\\workspace\\ecas\\src\\main\\resources\\upload\\triplet.txt";
        String data="";
        ArrayList<String>list=new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                list.add(line);

                // 在这里对每一行的内容进行处理
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常情况
        }


        //处理数据导出未语义对齐的json文件

        String []s1=new String[list.size()];
        int index=0;
        for(String str:list){
            s1[index]=str.substring(1,str.length()-1);
            index++;
        }

        //s2[i]装第i行2个实体和1个关系
        int lenx=s1.length;
        int leny=3;
        String [][]s2=new String[lenx][leny];
        for(int i=0;i<lenx;i++){
            s2[i]=s1[i].split(",");
        }

        ArrayList<String>entity=new ArrayList<>();
        return data;

    }




    @CrossOrigin
    @GetMapping(value="/jsonData/{id}")
    public String getJsonFiles(@PathVariable("id") int id){
        if(id!=7&&id!=8 && id!=9 && id!=10 &&id!=11){
            Random random = new Random();
            id=random.nextInt(5) + 7;
        }

        System.out.println("getJsonFiles");
        // TODO: 请用相对路径，如用绝对路径请修改
        String filePath="E:\\study\\study\\ecas\\ecas\\workspace\\ecas\\src\\main\\resources\\upload\\json"+id+".json";
        StringBuilder data= new StringBuilder();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                data.append(line).append("\n");

                // 在这里对每一行的内容进行处理
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常情况
        }
        return data.toString();

    }

    @GetMapping(value="/neo4jFile")
    public String exportJsonFromNeo4j() throws IOException {
        String uri = "bolt://localhost:7687";
        String user = "neo4j";
        String password = "123";

        //相对neo4j下文件夹import的路径
        String outputPath="'../../practiseTraining/ecasProject2/workspace/ecas/src/main/resources/files/result.json'";

        try (Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password))) {
            // 执行导出操作
            try (Session session = driver.session()) {
                String cypherQuery = "'MATCH (n) RETURN n'";
//                String outputPath="'file:/D:\\practiseTraining\\ecasProject2\\workspace\\ecas\\src\\main\\resources\\files\\result.json'";
                Result result = session.run("CALL apoc.export.json.all("+outputPath+","+"{}"+")");
                // 构建 JSON 字符串



                // 将 JSON 写入文件
                // 输出 JSON
//                System.out.println(result.toString());
            }
        }





//        Socket client=new Socket("127.0.0.1",8080);//创建客户端 本机ip地址，端口号随便输的
//
//        ///使用打印流包装 输出给服务器
//        PrintStream pos=new PrintStream(client.getOutputStream(),true);
//        ///客户端读取本地文件
//        FileInputStream fis=new FileInputStream("D:\\practiseTraining\\ecasProject2\\workspace\\ecas-vue\\src\\files\\result.json");
////	    ///使用缓冲流读本地文件
//        BufferedInputStream bis=new BufferedInputStream(fis);
//        byte bytes[]=new byte[1024];
//        int len;
//        System.out.println("读取本地文件");
//        while((len=bis.read(bytes))!=-1) {
//            ///客户端使用打印流输出给服务器
//            System.out.println("发送客户端文件");
//            pos.write(bytes,0,len);
//        }
//        client.shutdownOutput();//通知服务器，文件输入结束
//        BufferedInputStream cbis=new BufferedInputStream(client.getInputStream());
//        ///使用缓冲区包装 读取服务器信息
//        len=cbis.read(bytes);
//        System.out.println("服务器传来的信息："+new String(bytes,0,len));
//        pos.close();
//        bis.close();
//        cbis.close();
//        client.close();
        return "成功";
    }
}
